home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / nos042_s / at.c < prev    next >
C/C++ Source or Header  |  1994-09-16  |  7KB  |  299 lines

  1. /*    Timed execution routine. Starts a timer and executes a sequence
  2.  *    of commands when expired.
  3.  *
  4.  *    Added by IW0CNB - Feb 1992
  5.  *  'at mm' format added by WG7J - 920805
  6.  */
  7.  
  8. /****************************************************************************
  9. *    $Id: at.c 1.2 93/07/16 11:42:14 ROOT_DOS Exp $
  10. *    15 Jul 93    1.2        GT    Fix warnings.                                    *
  11. ****************************************************************************/
  12.  
  13. #include <time.h>
  14. #include <dos.h>
  15. #include <ext.h>
  16. #include "global.h"
  17. #include "timer.h"
  18. #include "cmdparse.h"
  19. #include "socket.h"
  20.  
  21. #if    defined (time)
  22. #undef    time                                /* remove macro                    */
  23. #endif
  24.  
  25. void                 atcmd(char *command);
  26. unsigned long     dostounix(struct date *dt, struct time *tm);
  27.  
  28.  
  29. /* List of events; We keep note of all timer processes generated by the 
  30.  * at command.
  31.  */
  32. struct at_list {
  33.     struct at_list *next;    /* Linked-list pointer */
  34.     struct timer *at_timer;
  35. };
  36.  
  37. #define    NULLATLIST    (struct at_list *)0
  38.  
  39. static struct at_list *Head_loe = NULLATLIST;    /* Head of List Of Events */
  40.  
  41. int doat(
  42.     int argc,
  43.     char *argv[],
  44.     void *p )
  45. {
  46.     char *cp;
  47.     struct date *exp_date;
  48.     struct time *exp_time;
  49.     struct timer *t;
  50.       time_t nowtime;
  51.     unsigned long time1;
  52.    struct tm tm;
  53.    extern struct timer *Timers;
  54.     struct at_list *loe;    /* List of events */
  55.     char *Errmsg = "Usage:\nat yymmddhhmm <cmd>\nat hhmm <cmd>\nat mm <cmd>\nat now+hhmm <cmd>\n";
  56.  
  57.     if (argc < 2) {                            /* Print list of pending at commands */
  58.         tputs("List of events:\n");
  59.         for (t = Timers; t != NULLTIMER; t = t->next)    {
  60.             if (t->func == (void (*)())atcmd)    {
  61.                 time(&nowtime);
  62.                 nowtime = (time_t)(read_timer(t) / 1000L + (unsigned long)nowtime);
  63.                 cp = ctime(&nowtime);
  64.                 rip(cp);
  65.                 tprintf("At: %s - Command: %s\n",cp,t->arg);
  66.            }
  67.         }
  68.         return 0;
  69.     }
  70.  
  71.     if (argc < 3)    {
  72.         tputs(Errmsg);
  73.         return 0;
  74.     }
  75.  
  76.     exp_date = (struct date *)mallocw(sizeof(struct date));
  77.     exp_time = (struct time *)mallocw(sizeof(struct time));
  78.  
  79.     cp=mallocw(5);
  80.  
  81.     switch (strlen(argv[1])) {
  82.     case 10:                                            /* Full date and time given         */
  83.         cp[0] = argv[1][0];
  84.         cp[1] = argv[1][1];
  85.         cp[2] = '\0';
  86.     
  87.         exp_date->da_year = 1900 + atoi(cp);
  88.         if(exp_date->da_year > 1999) goto error;
  89.  
  90.         cp[0] = argv[1][2];
  91.         cp[1] = argv[1][3];
  92.         cp[2] = '\0';
  93.  
  94.         exp_date->da_mon = (char)atoi(cp);
  95.         if(exp_date->da_mon > 12) goto error;
  96.  
  97.         cp[0] = argv[1][4];
  98.         cp[1] = argv[1][5];
  99.         cp[2] = '\0';
  100.  
  101.         exp_date->da_day = (char)atoi(cp);
  102.         if(exp_date->da_day > 31) goto error;
  103.  
  104.         cp[0] = argv[1][6];
  105.         cp[1] = argv[1][7];
  106.         cp[2] = '\0';
  107.  
  108.         exp_time->ti_hour = (char)atoi(cp);
  109.         if(exp_time->ti_hour > 23) goto error;
  110.  
  111.         cp[0] = argv[1][8];
  112.         cp[1] = argv[1][9];
  113.         cp[2] = '\0';
  114.  
  115.         exp_time->ti_min = (char)atoi(cp);
  116.         if(exp_time->ti_min > 59) goto error;
  117.  
  118.         exp_time->ti_sec = 0;
  119.         exp_time->ti_hund = 0;
  120.  
  121.         time(&nowtime);
  122.         time1 = (unsigned long)dostounix(exp_date,exp_time);
  123.         if(time1 < (unsigned long)nowtime) goto error;
  124.  
  125.         break;
  126.  
  127.     case 4:                              /* Only time given, so apply current date */
  128.         getdate(exp_date);
  129.         cp[0] = argv[1][0];
  130.         cp[1] = argv[1][1];
  131.         cp[2] = '\0';
  132.  
  133.         exp_time->ti_hour = (char)atoi(cp);
  134.         if(exp_time->ti_hour > 23) goto error;
  135.  
  136.         cp[0] = argv[1][2];
  137.         cp[1] = argv[1][3];
  138.         cp[2] = '\0';
  139.  
  140.         exp_time->ti_min = (char)atoi(cp);
  141.         if(exp_time->ti_min > 59) goto error;
  142.  
  143.         exp_time->ti_sec = 0;
  144.         exp_time->ti_hund = 0;
  145.  
  146.         time(&nowtime);
  147.         time1 = (unsigned long)dostounix(exp_date,exp_time);
  148.         if(time1 < (unsigned long)nowtime){    /* Requested time has passed */
  149.             time1 += 86400L;        /* So book him for tomorrow */
  150.         }
  151.         break;
  152.  
  153.  
  154.     case 2:  /* Only minutes given, so apply current time & date - WG7J */
  155.         tm.tm_min = (char)atoi(argv[1]);
  156.         if(tm.tm_min > 59) goto error;
  157.  
  158.       /* get today's date */
  159.  
  160.         getdate(exp_date);
  161.         tm.tm_year = exp_date->da_year - 1900;
  162.         tm.tm_mday = exp_date->da_day;
  163.         tm.tm_mon  = exp_date->da_mon - 1;
  164.  
  165.         /* get current time */
  166.  
  167.         gettime(exp_time);
  168.         tm.tm_hour = exp_time->ti_hour;
  169.  
  170.         /* if we're already past the minute, do it next hour ! */
  171.  
  172.         if (exp_time->ti_min > tm.tm_min)
  173.             tm.tm_hour++;
  174.  
  175.         /* now adjust this for day boundaries, etc. */
  176.  
  177.         tm.tm_sec = 0;
  178.         tm.tm_isdst = 0;
  179.         time1 = mktime(&tm);
  180.         time(&nowtime);
  181.         break;
  182.  
  183.     case 8:    /* now+hhmm given */
  184.         strncpy(cp,argv[1],4);
  185.         cp[4]='\0';
  186.  
  187.         if (strcmp(cp,"now+") != 0)
  188.             goto error;
  189.  
  190.         cp[0] = argv[1][4];
  191.         cp[1] = argv[1][5];
  192.         cp[2] = '\0';
  193.  
  194.         time1 = (unsigned long)atoi(cp)*3600L; 
  195.  
  196.         cp[0] = argv[1][6];
  197.         cp[1] = argv[1][7];
  198.         cp[2] = '\0';
  199.  
  200.         time1 += (unsigned long)atoi(cp)*60L;
  201.         time(&nowtime);
  202.         time1 += (unsigned long)nowtime;
  203.         break;
  204.  
  205.     default:
  206.     error:
  207.         tprintf(Errmsg);
  208.         free(exp_date);
  209.         free(exp_time);
  210.         free(cp);
  211.  
  212.         return 0;
  213.  
  214.     } /* switch */
  215.  
  216.     free(cp);
  217.     free(exp_time);
  218.     free(exp_date);
  219.  
  220.     t = (struct timer *)mallocw(sizeof(struct timer));
  221.  
  222.     set_timer(t,(time1 - (unsigned long)nowtime) * 1000L);
  223.     t->state = TIMER_RUN;
  224.     t->func  = (void (*)())atcmd;
  225.     t->arg   = (char *)mallocw(strlen(argv[2])+2);
  226.     strcpy(t->arg,argv[2]);
  227.     start_timer(t);
  228.  
  229.     /* Add the new timer to the head of List Of Events */
  230.  
  231.     loe = (struct at_list *)mallocw(sizeof(struct at_list));
  232.     loe->at_timer = t;
  233.     loe->next = Head_loe;
  234.     Head_loe = loe;
  235.  
  236.     return 0;
  237. }
  238.  
  239. /*
  240.     atcmd - Function to be called on timer expiration to execute a command
  241. */
  242.  
  243. void atcmd(char *command)
  244. {
  245.     extern struct cmds far Cmds[];
  246.     struct at_list *loe, *p;
  247.  
  248.     log(-1, "AT command: %s", command);
  249.  
  250.     /* Free up memory for expired at commands */
  251.     
  252.     p   = Head_loe;
  253.     loe = Head_loe;
  254.  
  255.     while(loe != NULLATLIST) {
  256.         if (loe->at_timer->state == TIMER_EXPIRE) {
  257.             free(loe->at_timer);                    /* Free timer                             */
  258.             if (loe == Head_loe) {
  259.                 Head_loe=loe->next;
  260.                 p=loe->next;
  261.                 free(loe);
  262.                 loe = p;
  263.                 p = Head_loe;
  264.             } else {
  265.                 p->next = loe->next;
  266.                 free(loe);
  267.                 loe = p->next;
  268.             }
  269.         } else {                                        /* Not expired, go on                 */
  270.             if (loe != Head_loe) p=p->next;
  271.             loe = loe->next;
  272.         }
  273.     }
  274.     cmdparse(Cmds,command,NULL);                /* Go with requested command */
  275.     free(command);
  276. }
  277.  
  278. /*
  279.     dostounix - convert time format
  280. */
  281.  
  282. unsigned long dostounix(
  283.     struct date *dt,
  284.     struct time *tm )
  285. {
  286.     char tbuf[6];
  287.  
  288.     /* setup time in UNIX unpacked format                                                */
  289.     
  290.    tbuf[0] = (char)dt->da_year - 1970;                /* year - 1970                 */
  291.     tbuf[1] = (char)dt->da_mon;                        /* month     (1 to 12)            */
  292.     tbuf[2] = (char)dt->da_day;                        /* day    (1 to 31)            */
  293.     tbuf[3] = (char)tm->ti_hour;                        /* hour   (0 to 23)            */
  294.     tbuf[4] = (char)tm->ti_min;                        /* minute (0 to 59)            */
  295.     tbuf[5] = (char)tm->ti_sec;                        /* second (0 to 59)            */
  296.  
  297.     return utpack(tbuf);                                    /* convert to packed form    */
  298. }
  299.